from celery import Celery
from celery.schedules import crontab

from app.settings import REDIS_HOST, REDIS_PORT, REDIS_AUTH

if REDIS_HOST == "127.0.0.1":
    redis_host = REDIS_HOST
else:
    redis_host = f':{REDIS_AUTH}@{REDIS_HOST}'
cel = Celery(
    'celery_demo',
    # broker='amqp://admin:admin@127.0.0.1:5672//',
    broker=f'redis://{redis_host}:{REDIS_PORT}/3',
    backend=f'redis://{redis_host}:{REDIS_PORT}/4',
    # 包含以下两个任务文件，去相应的py文件中找任务，对多个任务做分类
    include=[
        'tasks.task01',
        'tasks.crawling_task',
    ]
)

# 时区
cel.conf.timezone = 'Asia/Shanghai'
# 是否使用UTC
cel.conf.enable_utc = False
# celery作为一个单独项目运行，在settings文件中设置
cel.conf.broker_connection_retry_on_startup = True

# 定时循环任务
cel.conf.beat_schedule = {
    # 'add-every-5-seconds': {
    #     'task': 'celery_tasks.task01.send_email',
    #     # 'schedule': 1.0,
    #     # 'schedule': crontab(hour='12', minute='54'),
    #     'schedule': timedelta(seconds=1),
    #     'args': ()
    # },
}
